var core = (function ($) {
    var getRootPath = function() {
        var curWwwPath=window.document.location.href;
        var pathName=window.document.location.pathname;
        var pos=curWwwPath.indexOf(pathName);
        var localhostPaht=curWwwPath.substring(0,pos);
        var projectName=pathName.substring(0,pathName.substr(1).indexOf('/')+1);
        return(localhostPaht+projectName);
    };

    /**
     *  分页组件
     */
    var getPage = function (url, config, content) {
        $.ajax({
            url:url,
            type:'post',
            dataType:'json',
            data:config,
            success:function(res)
            {
                laypage({
                    cont: 'page1',
                    pages: Math.ceil(res.data.total / config.pageSize),
                    curr: config.pageNum || 1,
                    group: 5,
                    skip: true,
                    jump: function (obj, first) {
                        if (!first) {
                            config.pageNum = obj.curr;
                            getPage(url,config,content);
                        }
                    }
                });
                $('#tbody').html(content(res,config.pageNum));
            }
        });
    };

	//返回content数据到页面，currPage便于删除数据时好定位到该类，此处未用
	function parseUserList(res,currPage){
		var content = "";
		$.each(res.data.list, function (i, o) {
			content += "<tr>";
			content += "<td>"+o.customerName+"</td>";
			content += "<td>"+o.customerPhone+"</td>";
			content += "<td>"+o.customerDescription+"</td>";
            if (o.customerType == '1') {
                content += "<td>买油送车</td>";
            } else if (o.customerType == '2') {
                content += "<td>买油送油</td>";;
            } else {
                content += "<td>出错</td>";
            }
			content += "<td>"+o.totalAmount+"</td>";
			
            content += "<td>"+o.releaseAmount+"</td>";
            if (o.backByMonth == '6') {
                content += '<td>六期</td>';
            } else if (o.backByMonth == '12') {
                content += '<td>十二期</td>';
            }  else if (o.backByMonth == '18') {
                content += '<td>十八期</td>';
            }  else if (o.backByMonth == '24') {
                content += '<td>二十四期</td>';
            }  else if (o.backByMonth == '36') {
                content += '<td>三十六期</td>';
            } else {
                content += '<td>出错</td>';
            }
            if (o.isRelease == '1') {
                content += '<td>是</td>';
            } else if (o.isRelease == '0') {
                content += '<td>否</td>';
            }  else {
                content += '<td>出错</td>';
            }
			content += "<td>"+o.createTime+"</td>";
            if (o.isRelease == '1') {
                content += '<td class="text-center"><a class="btn btn-success btn-xs" href="/buyOil/calculateOil?id=' + o.id + '">详情</a>&nbsp;&nbsp;</td>';
            } else if (o.isRelease == '0') {
                content += '<td class="text-center"><a class="btn btn-success btn-xs"  href="/buyOil/calculateOil?id=' + o.id + '">详情</a>&nbsp;&nbsp;<a href="/buyOil/toUpdateClient?id='+o.id+'" class="btn btn-xs btn-danger ui-tooltip delete">修改</a></td>';
            } else {
                content += '<td class="text-center"><a class="btn btn-success btn-xs" href="/buyOil/calculateOil?id=' + o.id + '">详情</a>&nbsp;&nbsp;</td>';
            }
			content += "</tr>";
		});
		return content;
	}
	
	//从后台获取json数据
	var getUserListByPage = function(curr,data,url){
        
		data = '{"pageNum":"'+(curr || 1)+'","pageSize":"10",'+data+'}';
		//字符串转 json 对象
		var obj = eval("("+data+")");
		core.getPage(url,obj,parseUserList);
	};
	
    return {
        getRootPath: getRootPath,
        getPage: getPage,
        getUserListByPage:getUserListByPage
    };


})(jQuery);